$(function () {
  var console = window.console || { log: function () {} };
  var URL = window.URL || window.webkitURL;
  var $image = $('#image');
  var options = {
          aspectRatio: 1/1,
          viewMode:1,
          dragMode:'none',
          preview: '.img-preview',
          responsive:false,
          autoCropArea:0.4,
          movable:false,
          cropBoxResizable:false,
          restore:false,
          zoomable:false,
          autoCrop:true,//显示裁剪框
          // minContainerWidth: 320,
          // minContainerHeight: 200,
        };
      var originalImageURL = $image.attr('src');
      var uploadedImageName = 'cropped.jpg';
      var uploadedImageType = 'image/jpeg';
      var uploadedImageURL;
      $('#image').cropper({
      aspectRatio: 1/1,
      viewMode:1,
      dragMode:'move',
      autoCropArea:0.4,
      preview: '.img-preview',
      responsive:false,
      movable:false,
      zoomable:false,
      cropBoxResizable:false,
      restore:false,
      minCanvasWidth: 0,
      minCanvasHeight: 0,
      minCropBoxWidth: 100,
      minCropBoxHeight: 100,
      autoCrop:false,//显示裁剪框
      minContainerWidth: 320,
      minContainerHeight: 200,
    ready:function () {
        // console.log("ready");
    },
    cropstart: function (e) {
        // console.log("cropstart");
    },
    cropmove: function (e) {
        // console.log("cropmove");
    },
    cropend: function (e) {
        // console.log("cropend");
    },
    crop: function (e) {
        // console.log("crop");
    },
    zoom: function (e) {
        // console.log("zoom");
    },
    ready: function () {
      $(this).cropper('crop');
    },

    });

    var $inputImage = $('#inputImage');

    if (URL) {
      $inputImage.change(function () {
        var files = this.files;
        var file;
  
        if (!$image.data('cropper')) {
          return;
        }
  
        if (files && files.length) {
          file = files[0];
  
          if (/^image\/\w+$/.test(file.type)) {
            uploadedImageName = file.name;
            uploadedImageType = file.type;
            if (uploadedImageURL) {
              URL.revokeObjectURL(uploadedImageURL);
            }  
            uploadedImageURL = URL.createObjectURL(file);
            // uploadedImageURL = URL= "/System/UploadChatImage?ProjectId=@ViewBag.ProjectId";
            $image.cropper('destroy').attr('src', uploadedImageURL).cropper(options);
            $inputImage.val('');
          } else {
            window.alert('不是图片!');
          }
        }
      });
    } else {
      $inputImage.prop('disabled', true).parent().addClass('disabled');
    }
    function selectImage(){
  // cropper.loadImage(fileList[0]);
  var $inputImage = $('#inputImage');

  if (URL) {
    $inputImage.change(function () {
      var files = this.files;
      var file;

      if (!$image.data('cropper')) {
        return;
      }

      if (files && files.length) {
        file = files[0];

        if (/^image\/\w+$/.test(file.type)) {
          uploadedImageName = file.name;
          uploadedImageType = file.type;
          if (uploadedImageURL) {
            URL.revokeObjectURL(uploadedImageURL);
          }  
          uploadedImageURL = URL.createObjectURL(file);
          // uploadedImageURL = URL= "/System/UploadChatImage?ProjectId=@ViewBag.ProjectId";
          $image.cropper('destroy').attr('src', uploadedImageURL).cropper(options);
          $inputImage.val('');
        } else {
          window.alert('不是图片!');
        }
      }
    });
  } else {
    $inputImage.prop('disabled', true).parent().addClass('disabled');
  }
}
    $("#clear").on("click", function () {
      $('#image').cropper('clear');
    })
    $("#fa-rotate-left").on("click", function () {
      $('#image').cropper('rotate', 90);
    })
    $("#fa-rotate-right").on("click", function () {
      $('#image').cropper('rotate', -90);
    })
    $("#reset").on("click", function () {
      $('#image').cropper('reset');
    })
    $("#getData").on("click", function () {
      var cas=$('#image').cropper('getCroppedCanvas');
      var base64url=cas.toDataURL('image/jpeg');
      cas.toBlob(function (e) {
          // console.log(e);  //生成Blob的图片格式
      })
      let param = {base64url:base64url};
              axios.post('/user/profilePicture', param).then((res) => {
                  if(res.data.code == 200){
                    window.location.reload(); 
                  }else{
                    alert("图片太大")
                  }
              })
  })
  // 上传图片

});
// function selectImage(){
//   // cropper.loadImage(fileList[0]);
//   var $inputImage = $('#inputImage');

//   if (URL) {
//     $inputImage.change(function () {
//       var files = this.files;
//       var file;

//       if (!$image.data('cropper')) {
//         return;
//       }

//       if (files && files.length) {
//         file = files[0];

//         if (/^image\/\w+$/.test(file.type)) {
//           uploadedImageName = file.name;
//           uploadedImageType = file.type;
//           if (uploadedImageURL) {
//             URL.revokeObjectURL(uploadedImageURL);
//           }  
//           uploadedImageURL = URL.createObjectURL(file);
//           // uploadedImageURL = URL= "/System/UploadChatImage?ProjectId=@ViewBag.ProjectId";
//           $image.cropper('destroy').attr('src', uploadedImageURL).cropper(options);
//           $inputImage.val('');
//         } else {
//           window.alert('申达股份水电费水电费');
//         }
//       }
//     });
//   } else {
//     $inputImage.prop('disabled', true).parent().addClass('disabled');
//   }
// }
